<template>
  <div class="overdueMetting">
    <div class="selectCondition">
      <el-form :model="overdueMettingForm" ref="overdueMettingForm" :rules="overdueMettingRules">
        <el-row type="flex" class="row-bg" justify="start">
          <!-- 会议名称 -->
          <el-row type="flex" class="row-bg" justify="start" :gutter="10">
            <el-col>
              <el-form-item prop="meeting_name">
                <el-input placeholder="请输入会议名称" clearable v-model="overdueMettingForm.meeting_name"></el-input>
                <!-- <el-select v-model="overdueMettingForm.meeting_name"  clearable filterable placeholder="请选择会议名称">
                                    <el-option
                                        v-for="(items, indx) in MettingDataList.data"
                                        :key="indx"
                                        :label="items.name"
                                        :value="items.name"
                                    ></el-option>
                                </el-select> -->
              </el-form-item>
            </el-col>

            <!-- 会议状态 -->
            <el-col>
              <el-form-item prop="meeting_state">
                <el-select v-model="overdueMettingForm.meeting_state" clearable filterable placeholder="请选择会议状态">
                  <el-option label="未召开" :value="0"></el-option>
                  <el-option label="已召开" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 创建人-->
            <el-col>
              <el-form-item prop="meeting_principal">
                <el-input
                  placeholder="请输入会议主持人"
                  clearable
                  v-model="overdueMettingForm.meeting_principal"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 重置按钮 -->
          <el-button @click="reset('overdueMettingForm')" v-has="'M1107002'" size="small" class="reset_btn"
            >重置</el-button
          >
          <!-- 搜索按钮 -->
          <el-button type="primary" size="small" v-has="'M1107001'" @click="searchClick2">搜索</el-button>
        </el-row>
      </el-form>
    </div>
    <!-- 数据展示部分 -->
    <el-card class="mettingDataTable">
      <el-row type="flex" class="row-bg" justify="end">
        <el-popover placement="bottom-start" width="152" trigger="click">
          <!-- @change="selchange" -->
          <el-checkbox-group v-model="colOptions" :min="3">
            <el-checkbox v-for="item in MettingDataList.columns" :label="item.title" :key="item.title"></el-checkbox>
          </el-checkbox-group>

          <el-button slot="reference" type="primary" class="create_btn">字段管理</el-button>
        </el-popover>
        <!-- 新增 -->
        <el-button @click="createMetting()" type="primary" v-has="'M1107003'" size="small" class="create_btn"
          >新增</el-button
        >
      </el-row>
      <el-table
        border
        height="576"
        :data="MettingDataList.data"
        v-loading="MettingDataListLoading"
        style="width: 100% "
        ref="MettingDataListRef"
        class="MettingDataList_table"
        :cell-style="{ textAlign: 'center', height: '60px' }"
        :header-cell-style="{ textAlign: 'center', height: '50px', backgroundColor: '#FFFFFF' }"
      >
        <template v-for="(v, i) in MettingDataList.columns">
          <el-table-column :prop="v.field" :label="v.title" :key="i" v-if="v.istrue" :min-width="v.width">
            <template slot-scope="scope">
              <span class="link-type" v-if="v.title == '会议名称'" @click="switchToDetail(scope.row.id)">{{
                scope.row.name
              }}</span>
              <span v-else-if="v.title == '会议状态'">{{ scope.row.meetingState == 1 ? '已召开' : '未召开' }}</span>
              <span v-else-if="v.title == '会议时间'"> {{ scope.row.meetingTime | formatTimer }}</span>
              <span v-else>{{ scope.row[v.field] }}</span>
            </template>
          </el-table-column>
        </template>
        <!-- <el-table-column prop="name" label="会议名称" v-if="colSelect[0].istrue" min-width="150px">
                    <template slot-scope="scope">
                        <span class="link-type" @click="switchToDetail(scope.row.id)">{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="meetingState" label="会议状态" v-if="colSelect[1].istrue">
                    <template slot-scope="scope">
                        {{ scope.row.meetingState == 1 ? '已召开' : '未召开' }}

                    </template>
                </el-table-column>
                <el-table-column prop="meetingSite" label="会议地点" v-if="colSelect[2].istrue"> </el-table-column>
                <el-table-column prop="meetingTime" label="会议时间" v-if="colSelect[3].istrue">
                    <template slot-scope="scope">
                        {{ scope.row.meetingTime | formatTimer }}
                    </template>
                </el-table-column>
                <el-table-column prop="meetingPrincipal" label="创建人" v-if="colSelect[4].istrue"> </el-table-column>
                <el-table-column prop="lastModifierName" label="更新人" v-if="colSelect[5].istrue"> </el-table-column> -->
        <el-table-column label="操作" min-width="100">
          <template slot-scope="scope">
            <el-link :underline="false" v-has="'M1107005'">
              <el-tooltip effect="light" content="编辑" placement="bottom-start">
                <span @click="editRow(scope.row)" class="option_update"></span>
              </el-tooltip>
              <!-- <el-tooltip content="编辑" placement="top" effect="light">
                                <span
                                    class="el-icon-edit"
                                    style="cursor: pointer;font-size:17px;margin-right:10px;"
                                    @click="editRow(scope.row)"
                                >
                                </span>
                            </el-tooltip> -->
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 控制数据展示数量的分页器 -->
    <div class="paginationClock">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pageIndex"
        :total="totalNumber"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryinfo.pageRow"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
    <!-- 弹出框 -->
    <CommonDialog
      :title="modelNum == 1 ? '新增' : '编辑'"
      width="40%"
      v-bind:dialogVisible="mettingDialogVisible"
      @on-change-dialogVisible="mettingDialogChange('mettingForm')"
      class="metting"
      top="10vh"
    >
      <template #body>
        <el-form
          class="mettingForm"
          ref="mettingForm"
          :model="mettingForm"
          :rules="mettingFormRules"
          label-position="right"
          size="mini"
          label-width="100px"
        >
          <el-form-item label="会议名称" prop="name">
            <el-input v-model="mettingForm.name" placeholder="请输入会议名称"></el-input>
          </el-form-item>

          <el-form-item label="会议状态" prop="meetingState">
            <el-select
              v-model="mettingForm.meetingState"
              clearable
              filterable
              style="width: 100%;"
              placeholder="请选择会议状态"
            >
              <el-option label="未召开" :value="0"></el-option>
              <el-option label="已召开" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="会议地点" prop="meetingSite">
            <el-input v-model="mettingForm.meetingSite" placeholder="请输入会议地点"></el-input>
          </el-form-item>
          <el-form-item label="会议时间" prop="meetingTime">
            <el-date-picker
              v-model="mettingForm.meetingTime"
              style="width: 100%;"
              type="date"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="主持人" prop="meetingPrincipal">
            <el-input v-model="mettingForm.meetingPrincipal" placeholder="请输入主持人"></el-input>
          </el-form-item>
        </el-form>
      </template>

      <template #dialog_footer>
        <el-button size="small" @click="cancleMetting('mettingForm')">取 消</el-button>

        <el-button v-if="modelNum == '1'" type="primary" size="small" @click="InsertAsync('mettingForm')"
          >保存</el-button
        >
        <el-button v-else type="primary" size="small" @click="UpdateAsync('mettingForm')">保存</el-button>
      </template>
    </CommonDialog>
  </div>
</template>

<script>
/* 引入通用的弹出框 */
import CommonDialog from '../../components/commonDialog/commonDialog.vue'
// 反馈列表
import { DelateMetting } from '../../api/delateMeeting/server'
import FilterTrans from '../../utils/paramsFiler'
import { setTimeout } from 'timers'
export default {
    name: '',
    components: {
        CommonDialog
    },
    props: {},
    data() {
        const EFilterOprator = FilterTrans.EFilterOprator
        return {
            queryinfo: {
                pageIndex: 1,
                pageRow: 50
            },
            totalNumber: null,
            // 筛选部分---------------
            //   筛选条件Form数据
            overdueMettingForm: {
                meeting_name: '',
                meeting_state: '',
                meeting_principal: ''
            },
            searchCondition: [
                {
                    field: 'meeting_name',
                    operator: EFilterOprator.Like
                },
                {
                    field: 'meeting_state',
                    operator: EFilterOprator.Equal
                },
                {
                    field: 'meeting_principal',
                    operator: EFilterOprator.Equal
                }
            ],
            overdueMettingRules: {},
            //   表格部分--------------------------
            // 表格列展现
            colOptions: [],
            colSelect: [],
            MettingColData: [
                { title: '会议名称', istrue: true },
                { title: '会议状态', istrue: true },
                { title: '会议地点', istrue: true },
                { title: '会议时间', istrue: true },
                { title: '主持人', istrue: true },
                { title: '更新人', istrue: true }
            ],
            // 会议表格数据list
            MettingDataList: {
                data: [],
                columns: [
                    { title: '会议名称', field: 'name', width: 120, istrue: true },
                    { title: '会议状态', field: 'meetingState', width: 120, istrue: true },
                    { title: '会议地点', field: 'meetingSite', width: 120, istrue: true },
                    { title: '会议时间', field: 'meetingTime', width: 120, istrue: true },
                    { title: '主持人', field: 'meetingPrincipal', width: 120, istrue: true },
                    { title: '更新人', field: 'lastModifierName', width: 120, istrue: true }
                ]
            },
            MettingDataListLoading: false,

            //   弹出框部分-------------------------
            mettingId: '',
            mettingDialogVisible: false,
            modelNum: '1', // 新增1，编辑2
            mettingForm: {
                name: '', // 会议名称
                meetingState: 0, // 会议状态
                meetingSite: '', // 会议地点
                meetingTime: '', // 会议时间
                meetingPrincipal: '', // 创建人
                lastModifierName: '' // 更新人
            },
            mettingFormRules: {
                name: [{ required: true, message: '请输入会议名称', trigger: 'change' }],
                editname: [{ required: false, message: '请输入会议名称', trigger: 'change' }],
                meetingState: [{ required: false, message: '请选择会议状态', trigger: 'change' }],
                meetingSite: [{ required: false, message: '请输入会议地点', trigger: 'change' }],
                meetingTime: [{ required: false, message: '请选择会议时间', trigger: 'change' }],
                meetingPrincipal: [{ required: false, message: '请输入会议主持人', trigger: 'change' }],
                lastModifierName: [{ required: true, message: '请输入更新人', trigger: 'change' }]
            },
            isload: false
        }
    },
    filters: {
        formatTimer: function(value) {
            if (value) {
                const date = new Date(value)
                const y = date.getFullYear()
                let MM = date.getMonth() + 1
                MM = MM < 10 ? '0' + MM : MM
                let d = date.getDate()
                d = d < 10 ? '0' + d : d
                let h = date.getHours()
                h = h < 10 ? '0' + h : h
                let m = date.getMinutes()
                m = m < 10 ? '0' + m : m
                let s = date.getSeconds()
                s = s < 10 ? '0' + s : s
                return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
            } else {
                return value
            }
        }
    },
    watch: {
    //  监听表格显示项
        colOptions(valArr) {
            var arr = this.colSelect.filter(i => valArr.indexOf(i) < 0) // 未选中
            console.log('arr', arr)
            this.MettingDataList.columns.filter(i => {
                if (arr.indexOf(i.title) != -1) {
                    i.istrue = false
                } else {
                    i.istrue = true
                }
            })
            localStorage.setItem('mettingList', JSON.stringify(this.colOptions))
            this.$nextTick(() => {
                this.$refs.MettingDataListRef.doLayout()
            })
        }
    },
    created() {
    //  创建表格显示项
        var _this = this
        for (let i = 0; i < _this.MettingDataList.columns.length; i++) {
            _this.colSelect.push(_this.MettingDataList.columns[i].title)
            if (_this.MettingDataList.columns[i].istrue == false) {
                // 初始化不想展示的列可以放在这个条件里
                continue
            }
            _this.colOptions.push(_this.MettingDataList.columns[i].title)
        }
        // for (let i = 0; i < _this.MettingColData.length; i++) {
        //     // _this.colSelect.push({
        //     //     title: _this.MettingColData[i].title,
        //     //     istrue: _this.MettingColData[i].istrue

        //     // })

        //     _this.colOptions.push(_this.MettingColData[i].title)
        // }
    },
    mounted() {
        this.searchClick()
        // 将用户的配置习惯渲染
        const settingArr = JSON.parse(localStorage.getItem('mettingList'))
        if (settingArr != null) {
            this.colOptions = settingArr
        }
    },
    methods: {
        selchange(val) {
            console.log(val)
            var arr2 = this.colOptions.filter(i => val.indexOf(i.title) < 0) // 未选中
            this.MettingDataList.columns.filter(i => {
                if (arr2.indexOf(i.title) != -1) {
                    i.istrue = true
                } else {
                    i.istrue = false
                }
            })
            console.log('arr2', arr2)
            console.log(this.MettingDataList.columns)
            this.MettingDataList.columns = this.MettingDataList.columns.sort(this.compare('istrue'))
            console.log(this.MettingDataList.columns)
        },
        compare(property) {
            return function(a, b) {
                var value1 = a[property]
                var value2 = b[property]
                return value2 - value1
            }
        },
        searchClick2() {
            this.queryinfo.pageIndex = 1
            this.searchClick()
        },

        // 搜索
        searchClick() {
            this.filter = FilterTrans.buildPageRequest(this.overdueMettingForm, this.searchCondition)
            this.filter.orderConditions = [
                {
                    sortDirection: 1,
                    sortField: 'created_at'
                }
            ]
            this.GetPageAsync()
        },
        // 会议分页列表
        async GetPageAsync() {
            try {
                const data = this.handleForm(this.filter)
                const result = await DelateMetting.GetPageAsync(data)
                if (result.success) {
                    this.totalNumber = result.data.total
                    this.MettingDataListLoading = true
                    setTimeout(() => {
                        this.MettingDataListLoading = false
                        this.MettingDataList.data = result.data.data
                    }, 1000)
                }
            } catch (err) {
                console.log(err)
            }
        },
        // 新建会议保存
        InsertAsync(formName) {
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    try {
                        const result = await DelateMetting.InsertAsync(this.mettingForm)
                        if (result.success) {
                            console.log(result.data)
                            this.mettingDialogVisible = false
                            // this.searchClick();
                            this.reset(formName)
                            this.searchClick()
                            this.$message.success('新建成功')
                        } else {
                            this.$message.error(result.message)
                        }
                    } catch (err) {
                        this.$message.error('服务器异常')
                    }
                } else {
                    this.$message.error('请检查表单信息是否填写完整')
                    return false
                }
            })
        },
        // 编辑更新
        UpdateAsync(formName) {
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    try {
                        const result = await DelateMetting.UpdateAsync(this.mettingForm)
                        if (result.success) {
                            console.log(result.data)
                            this.mettingDialogVisible = false
                            this.reset(formName)
                            this.searchClick()
                        } else {
                            this.$message.error(result.message)
                        }
                    } catch (err) {
                        this.$message.error('服务器异常')
                    }
                } else {
                    this.$message.error('请检查表单信息是否填写完整')
                    return false
                }
            })
        },
        // 获取编辑行信息
        async GetLoadAsync(_id) {
            try {
                const result = await DelateMetting.GetLoadAsync(_id)
                if (result.success) {
                    console.log(result.data)
                    console.log('Success')
                    this.mettingForm = result.data
                }
            } catch (err) {
                console.log(err)
            }
        },
        handleForm(_filter) {
            const filters = FilterTrans.filterParam(_filter.queryFilter.filters)
            const filter = JSON.parse(JSON.stringify(_filter))
            filter.queryFilter.filters = filters
            filter.pageIndex = this.queryinfo.pageIndex
            filter.pageRow = this.queryinfo.pageRow
            return filter
        },

        reset(formName) {
            this.$refs[formName].resetFields()
            this.queryinfo.pageIndex = 1
            this.mettingForm = {
                id: '',
                name: '', // 会议名称
                meetingState: 0, // 会议状态
                meetingSite: '', // 会议地点
                meetingTime: '', // 会议时间
                meetingPrincipal: '', // 创建人
                lastModifierName: '' // 更新人
            }
        },
        handleSizeChange(_val) {
            console.log(_val)
            this.queryinfo.pageRow = _val
            console.log(this.queryinfo)
            this.searchClick()
        },

        handleCurrentChange(_val) {
            console.log(_val)
            this.queryinfo.pageIndex = _val
            this.searchClick()
        },
        // 新建会议弹出框
        createMetting() {
            this.modelNum = '1'
            this.mettingDialogVisible = true
        },

        // 新建会议取消
        cancleMetting(formName) {
            this.mettingDialogVisible = false
            this.$refs[formName].resetFields()
        },
        // 编辑会议弹出框
        editRow(row) {
            this.mettingId = row.id
            this.GetLoadAsync(row.id)
            this.modelNum = '2'
            this.mettingDialogVisible = true
        },
        // 跳转到
        switchToDetail(id) {
            this.$router.push({ path: 'mettingDetail?id=' + id })
        },
        mettingDialogChange(formName) {
            this.mettingDialogVisible = false
            this.$refs[formName].resetFields()
            this.mettingForm = {
                id: '',
                name: '', // 会议名称
                meetingState: 0, // 会议状态
                meetingSite: '', // 会议地点
                meetingTime: '', // 会议时间
                meetingPrincipal: '', // 创建人
                lastModifierName: '' // 更新人
            }
        }
    }
}
</script>

<style scoped lang="less">
.overdueMetting {
  padding: 30px;
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  .selectCondition {
    display: flex;
    align-items: center;
    padding: 15px 30px;
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    background: #ffffff;
    box-shadow: 0px 2px 5px 0px rgba(152, 160, 179, 0.2);
    border-radius: 5px;
    .row-bg {
      align-items: center;
      .options {
        display: flex;
        justify-content: flex-end;
        align-items: center;
      }
    }
    ::v-deep .el-form-item {
      margin-bottom: 0;
      max-width: 150px;
    }
    ::v-deep .el-input__inner {
      height: 30px;
      line-height: 30px;
    }
  }
  .mettingDataTable {
    height: 645px;
    margin-top: 30px;
    padding: 0 30px;
    // padding-right: 20px;
    box-sizing: border-box;
    overflow: hidden;
    background-color: #ffffff;
    ::v-deep .el-card__body {
      padding: 0;
    }
    .MettingDataList_table {
      box-sizing: border-box;
    }
  }
  .paginationClock {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 25px 0;
    ::v-deep .el-input__inner {
      width: 100%;
    }
  }
  ::v-deep .metting .el-dialog__header {
    width: 100% !important;
  }
  ::v-deep .metting .el-dialog__body {
    padding: 10px 30px;
    height: 300px;
    overflow: auto;
  }
  ::v-deep .metting .el-dialog {
    border-radius: 5px;
    height: 460px;
    box-sizing: border-box;
  }
}

::v-deep .el-table td,
.el-table th {
  padding: 0;
}
::v-deep .el-table th {
  background-color: #f6f6f6 !important;
}
::v-deep .el-table th {
  font-size: 14px;
  color: #3579e0;
  font-weight: 400;
}
::v-deep .el-table th.gutter {
  display: none;
}
.create_btn {
  margin: 10px 10px 10px 0;
}

.reset_btn {
  margin-left: 20px;
}
.option_export,
.option_look,
.option_edit,
.option_delete,
.option_suspend,
.option_confirm,
.option_reback,
.option_close,
.option_update,
.option_life,
.option_more {
  display: inline-block;
  width: 16px;
  height: 16px;
  margin: 0 5px;
  position: relative;
  cursor: pointer;
  top: 5px;
}
.option_update {
  background: url('~@/assets/image/options/Ican_9_Selected@2x.png');
  background-size: contain;
}

.link-type,
.link-type:focus {
  color: rgb(32, 160, 255);
  cursor: pointer;
  text-decoration: underline;
  &:hover {
    color: #337ab7;
  }
}
</style>
